<template>
  <div class="Banner">
    <div class="header">
      <div class="isShow">
        <el-switch
          v-model="value"
          active-color="#409EFF"
          inactive-color="#DCDFE6"
          active-text="前台显示新闻模块"
          inactive-text="前台不显示新闻模块"
          @change="changeIsShowNews"
        ></el-switch>
        <!-- <el-switch v-model="value" active-color="#409EFF" inactive-color="#DCDFE6"></el-switch> -->
      </div>
      <el-button type="primary" @click="addCase">新增新闻</el-button>
    </div>
    <div class="tabels">
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="title" label="标题" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="summary" label="简介" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createTime" label="创建时间" :formatter="formatter" align="center"></el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="dialogTitle"
      top="1vh"
      ref="dialog"
      :close-on-click-modal="false"
      :visible.sync="isShowDialog"
    >
      <div class="each cover">
        <b>*</b>
        <span>封面:</span>
        <img :src="obj.cover" alt="封面" v-if="obj.cover" />
        <Upload :name="obj.cover?'重新上传':'上传图片'" type="news" @saveCb="onUploadSuccess"></Upload>
      </div>
      <div class="each">
        <b>*</b>
        <span>标题:</span>
        <el-input v-model="obj.title" placeholder="请输入标题"></el-input>
      </div>
      <div class="each">
        <b>*</b>
        <span>简介:</span>
        <el-input v-model="obj.summary" placeholder="请输入简介"></el-input>
      </div>
      <div class="each1">
        <b>*</b>
        <span>内容:</span>
        <Quill ref="quill" :editContent="obj.content" :height="500"></Quill>
      </div>
      <div class="btns">
        <div class="btn">
          <el-button type="info" @click="isShowDialog=false">取消</el-button>
        </div>
        <div class="btn">
          <el-button type="primary" @click="save">确定</el-button>
        </div>
      </div>
    </el-dialog>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSie"
        :page-sizes="[10, 20, 30, 40,50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      ></el-pagination>
    </div>
    <MyDialog title="删除" ref="deleteCase" lastText="删除" :colorType="true" @saveHandle="deleteCase">
      <p class="tips">确定删除吗？</p>
      <p>删除后此新闻前台将不再显示</p>
    </MyDialog>
  </div>
</template>
<script>
import Quill from "../components/quill";
import Upload from "../components/upload";
import MyDialog from "../components/dialog";
export default {
  name: "Banner",
  components: {
    Quill,
    Upload,
    MyDialog
  },
  data() {
    return {
      value: false,
      pageSie: 10,
      count: 0,
      currentPage: 1,
      list: [
        {
          id: 1,
          title: "我是标题",
          brief: "我是简介",
          creatTime: 1601199686000
        }
      ],
      isShowDialog: false,
      dialogTitle: "新增新闻",
      obj: {
        cover:
          "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1593106255,4245861836&fm=26&gp=0.jpg",
        title: "我是标题",
        summary: "我是简介",
        content: ""
      },
      saveType: "add",
      did: ""
    };
  },
  methods: {
    //切换是否显示新闻模块
    changeIsShowNews(val) {
      if (val) {
        this.$post("/news/module/show").then(res => {
          if (res.code == 200) {
            this.$message.success("已设置为显示");
          }
        });
      } else {
        this.$post("/news/module/hidden").then(res => {
          if (res.code == 200) {
            this.$message.success("已设置为隐藏");
          }
        });
      }
    },
    handleEdit(row) {
      this.obj = {
        cover: "",
        title: "",
        summary: "",
        content: ""
      };
      this.isShowDialog = true;
      this.did = row.did;
      this.$post("/news/info", { did: row.did }).then(res => {
        if (res.code == 200) {
          this.dialogTitle = "编辑新闻";
          this.saveType = "edit";
          const { data } = res;
          this.obj = data.news;
          this.$nextTick(() => {
            this.$refs.quill.onShow();
          });
        }
      });
    },
    handleDelete(row) {
      this.did = row.did;
      this.$refs.deleteCase.show();
    },
    //确认删除
    deleteCase() {
      this.$post("/news/delete", { did: this.did }).then(res => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.$refs.deleteCase.hide();
          this.getList();
        }
      });
    },
    //时间搓处理
    formatter(row, column) {
      let pro = column.property;
      let dates = row[pro];
      if (dates == "0") {
        return "-";
      } else {
        var date = new Date(dates);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? "0" + m : m;
        var d = date.getDate();
        d = d < 10 ? "0" + d : d;
        let hour = date.getHours();
        if (hour < 10) {
          hour = "0" + hour;
        }
        let minute = date.getMinutes();
        if (minute < 10) {
          minute = "0" + minute;
        }
        let second = date.getSeconds();
        if (second < 10) {
          second = "0" + second;
        }
        return (
          y + "-" + m + "-" + d + "\xa0 " + hour + ":" + minute + ":" + second
        );
      }
    },
    addCase() {
      this.isShowDialog = true;
      this.dialogTitle = "新增新闻";
      this.saveType = "add";
      this.obj = {
        cover: "",
        title: "",
        summary: "",
        content: ""
      };
      this.$nextTick(() => {
        this.$refs.quill.onShow();
      });
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSie = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    onUploadSuccess(val) {
      this.obj.cover = val;
    },
    getList() {
      this.$post("/news/list", {
        page: this.currentPage,
        pageSie: this.pageSie
      }).then(res => {
        if (res.code == 200) {
          const { data } = res;
          this.count = data.count;
          this.list = data.news;
        }
      });
    },
    save() {
      this.obj.content = this.$refs.quill.saveHtml();
      let obj = this.obj;
      if (!obj.cover) {
        this.$message.error("请设置封面");
        return;
      }
      if (!obj.title) {
        this.$message.error("请输入标题");
        return;
      }
      if (!obj.summary) {
        this.$message.error("请输入简介");
        return;
      }
      if (!obj.content) {
        this.$message.error("请设置内容");
        return;
      }
      if (this.saveType == "add") {
        this.$post("/news/create", this.obj).then(res => {
          if (res.code == 200) {
            this.$message.success("新增成功");
            this.isShowDialog = false;
            this.getList();
          } else if (res.code == 95001) {
            this.$message.error("新闻标题重复");
          }
        });
      } else {
        this.$post("/news/update", {
          did: this.did,
          title: obj.title,
          cover: obj.cover,
          summary: obj.summary,
          content: obj.content
        }).then(res => {
          if (res.code == 200) {
            this.$message.success("编辑成功");
            this.isShowDialog = false;
            this.getList();
          }
        });
      }
    },
    //查询新闻模块是否显示
    getInfo() {
      this.$post("/news/module/info").then(res => {
        if (res.code == 200) {
          const { data } = res;
          this.value = data.module.showStatus == 1 ? true : false;
        }
      });
    }
  },
  created() {
    this.getInfo();
    this.getList();
  },
  mounted() {
    this.$parent.setPannelName([
      {
        name: "新闻管理",
        path: "/News",
        des: "本页面用于新闻模块新增修改删除等操作"
      }
    ]);
  }
};
</script>
<style lang="less" scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: right;
  padding: 10px 30px 10px 10px;
  border-bottom: 1px solid #ccc;
  .isShow {
    margin-right: 20px;
  }
}
.tabels {
  padding: 0 30px;
}
.each {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  b {
    color: #fe6972;
  }
  > span {
    display: inline-block;
    width: 60px;
    margin-right: 20px;
  }
  > img {
    width: 105px;
    height: 120px;
    margin-right: 20px;
  }
}
.each1 {
  b {
    color: #fe6972;
  }
}
.btns {
  display: flex;
  margin-top: 20px;
  justify-content: center;
  .btn {
    margin-right: 20px;
  }
}
.file {
  position: relative;
  display: inline-block;
  background: #d0eeff;
  border: 1px solid #99d3f5;
  border-radius: 4px;
  padding: 4px 12px;
  overflow: hidden;
  color: #1e88c7;
  text-decoration: none;
  text-indent: 0;
  line-height: 20px;
}
.file input {
  position: absolute;
  font-size: 100px;
  right: 0;
  top: 0;
  opacity: 0;
}
.file:hover {
  background: #aadffd;
  border-color: #78c3f3;
  color: #004974;
  text-decoration: none;
}
.page {
  text-align: center;
  margin-top: 20px;
}
.tips {
  font-weight: normal;
  font-size: 32px;
  margin-bottom: 50px;
}
</style>
